﻿/**
*    @file:    menucontroller.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-11-05  22:52
*/
#include "menucontroller.h"

#include <QJsonDocument>
#include <menuservice.h>
#include <menuvo.h>
#include <qjwtutils.h>
#include <roleservice.h>
#include <user.h>
#include "responesutils.h"

MenuController::MenuController(QObject *parent) : HttpController(parent)
{

}

MenuController::~MenuController()
{

}

void MenuController::route(QString path, QString method, HttpRequest &request, HttpResponse &response)
{
    qDebug() << __FUNCTION__ << path << method;
    if (method == "GET")
    {
        if (path == "all")
        {
            return getAll(request, response);
        }
        else if (path == "list")
        {
            return getList(request, response);
        }
        else if(path == "name-exists")
        {
            return isMenuNameExists(request, response);
        }
        else if(path == "path-exists")
        {
            return isMenuPathExists(request, response);
        }
    }
    else if (method == "POST")
    {
        if (path.isEmpty())
        {
            return saveOne(request, response);
        }
    }
    else if (method == "DELETE")
    {
       return deleteOne(path.toULongLong(), response);
    }
    else if (method == "PUT")
    {
        return saveOne(request, response, path.toULongLong());
    }
}

void MenuController::getAll(HttpRequest &request, HttpResponse &response)
{
    QString authHeader = request.getHeader("Authorization");
    //权限找不到或者权限字符串头找不到
    if(authHeader.isEmpty() || authHeader.indexOf("Bearer ") == -1 || authHeader.split(" ").size() < 2){
        qDebug() << "权限找不到或者权限字符串头找不到";
        return ResponesUtils::unAuthorizedResponse(response);

    }
    QString token = authHeader.split(" ")[1];
    QJsonObject jsonObject= QJwtUtils::verifyAccessToken(token);
    User user;
    user.fromJsonObject(jsonObject);
    if(user.isEmpty()){
        return ResponesUtils::unAuthorizedResponse(response);
    }
    QList<MenuVO> list;
    MenuService objectService;
    if(user.getRoles().contains("admin")){
        if(!objectService.getMenuVoList(list)){
              return ResponesUtils::erroResponse(response,"数据库异常！");
        }
    }else{
        RoleService roleService;
        QList<QString> permissions;
        if(!roleService.getPermissionsByCodes(permissions,user.getRoles())){
            return ResponesUtils::erroResponse(response,"数据库异常！");
        }

        if(permissions.size() == 0){
            return ResponesUtils::erroResponse(response,"暂未授权，请联系管理员分配权限！");
        }

        if(!objectService.getMenuVoListByPermissions(list,permissions)){
              return ResponesUtils::erroResponse(response,"数据库异常！");
        }
    }
    QJsonArray jsonArray;
    for(MenuVO &item : list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void MenuController::getList(HttpRequest &request, HttpResponse &response)
{
    MenuService objectService;
    QList<MenuVO> list;
    if(!objectService.getMenuVoList(list)){
          return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    QJsonArray jsonArray;
    for(MenuVO &item : list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void MenuController::isMenuNameExists(HttpRequest &request, HttpResponse &response)
{
    QString name = request.getParameter("name");
    uint64_t id = request.getParameter("id").toULongLong();
    if(name.isEmpty()){
        return ResponesUtils::resultResponse(response,false);
    }
    Menu object;
    object.setName(name);
    MenuService objectService;
    if(!objectService.getOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    if(id == object.getId()){
        return ResponesUtils::resultResponse(response,false);
    }else{
        return ResponesUtils::resultResponse(response,object.getId() != 0);
    }
}

void MenuController::isMenuPathExists(HttpRequest &request, HttpResponse &response)
{
    QString path = request.getParameter("path");
    uint64_t id = request.getParameter("id").toULongLong();
    if(path.isEmpty()){
        return ResponesUtils::resultResponse(response,false);
    }
    Menu object;
    object.setPath(path);
    MenuService objectService;
    if(!objectService.getOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    if(id == object.getId()){
        return ResponesUtils::resultResponse(response,false);
    }else{
        return ResponesUtils::resultResponse(response,object.getId() != 0);
    }
}

void MenuController::saveOne(HttpRequest &request, HttpResponse &response, uint64_t id)
{
    QByteArray body = request.getBody();
    QJsonParseError parseError;
    QJsonDocument  jsonDocument= QJsonDocument::fromJson(body, &parseError);

    if (parseError.error != QJsonParseError::NoError)
    {
        return ResponesUtils::erroResponse(response,"请求参数JSON结构异常!");
    }

    MenuVO objectVO;
    Menu object;
    objectVO.fromJsonObject(jsonDocument.object());
    objectVO.toMenu(object);
    if(id != 0){
       object.setId(id);
    }
    MenuService objectService;
    if(!objectService.saveOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,id == 0 ? "添加成功！" : "修改成功！");
}

void MenuController::deleteOne(uint64_t id, HttpResponse &response)
{
    if(id == 0 ){
        return ResponesUtils::erroResponse(response,"请指定删除ID！");
    }
    Menu object;
    object.setId(id);
    MenuService objectService;
    if(!objectService.deleteOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"删除成功！");
}


